//
//  ImagesController.m
//  Words2
//
//  Created by Mathieu Tozer on 7/09/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "ImagesController.h"
#import "ImageMO.h"

@implementation ImagesController

-(void)awakeFromNib{
//	[self registerAsObserver];
}

- (void)registerAsObserver
{
    [self addObserver:self
		   forKeyPath:@"arrangedObjects"
			  options:(NSKeyValueObservingOptionNew |
					   NSKeyValueObservingOptionOld)
			  context:NULL];
}


- (void)observeValueForKeyPath:(NSString *)keyPath
					  ofObject:(id)object 
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if (object == self && [keyPath isEqual:@"arrangedObjects"]) {
		
		/*
		 Everytime the controller is re arranged, prepare a new string of imageLocations for the quartzcomposer view, with the file: prefix
		 */ 
		[slideTimer invalidate];
		NSArray *images = [self arrangedObjects];
		NSMutableArray *imageLocations = [[NSMutableArray alloc] init];
		int count = [images count];
		NSEnumerator *imgEnum = [images objectEnumerator];
		ImageMO *nextImg;
		if(count == 0){
		}else{
			while (nextImg = [imgEnum nextObject]){
				NSString *location = [nextImg valueForKey:@"imageLocation"];
				if(location != nil){
					[imageLocations addObject:location]; //extract the locations as they are represented in the core data model
				}
			}
			
			NSMutableString *imageFiles = [[NSMutableString alloc] initWithString:@""];
			NSEnumerator *e = [imageLocations objectEnumerator];
			NSString *nextLoc;
			NSMutableArray *fullFilesForSlideShow = [[NSMutableArray alloc] init];
			while(nextLoc = [e nextObject]){
				imageFiles = [[NSMutableString alloc] initWithString:@""];
				[imageFiles appendString:@"file://"];
				[imageFiles appendString:[[NSHomeDirectory() stringByAppendingString:nextLoc] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
				[fullFilesForSlideShow addObject:imageFiles];
			}
			
			//NSLog(@"Starting a new slideshow with images located at: %@", fullFilesForSlideShow);
			[self setUpNewSlideShowWithImages:fullFilesForSlideShow];
		//	[myLocationsString release];
			
		}
		
		// observe the same change
		[super observeValueForKeyPath:keyPath
							 ofObject:object 
							   change:change 
							  context:context];
	}
}


- (IBAction)keepCurrentSelection:(id)sender{
	//flag the current image to persist in the store.
}

-(void)setUpNewSlideShowWithImages:(NSArray *)imagesArray{
	slideShowPaths = [[NSMutableArray alloc] initWithArray:imagesArray]; //slide show paths is our over arching NSArrayController for image paths. 
	i = 0; //start from the start (might change this later)
	count = [slideShowPaths count];
	float timeInterval = 7.0; //seconds
 	slideTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval 
														   target:self selector:@selector(changeSlideShowImageToNextImage) 
														 userInfo:nil repeats:YES];
}

-(void)changeSlideShowImageToNextImage{
	NSLog(@"Changing the slide.");
	if(i <= count-1){
		i++;
	}else{
		i = 0;
	}
	[imagesString setStringValue:[slideShowPaths objectAtIndex:i]];
}

// take an NSImage and add it to the core data model, probably to use in drag and drops of images by the user.
//-(void)addImage:(NSImage *)newImage{
//	
//}
//-(void)removeImage:(NSImage *)image{
//	
//}
//
//// accessor to search the services as specified in the user preferences and returns an array of NSImages
////-(NSArray *)arrayOfImagesForWord:(WordMO *)word{
////	// get the user preferences for which services to search for
////	//check they are available
////	//connect to the services
////	//for each service, loop around and download images into a mutable array
////	//call the [self addImages] method to add the mutable array to the currently selected word.
////}
//
//// method to add an array of images to a word.
//-(void)addImages:(NSArray *)images toWord:(WordMO *)word{
//	
//}


@end
